package link.anyauto.study;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import link.anyauto.study.databinding.ChildBinding;
import link.anyauto.study.databinding.GroupBinding;

/**
 * Created by LYQ on 2016/7/19.
 */
public class ExpandableAdapter extends BaseExpandableListAdapter {

    Data data;

    @Override
    public int getGroupCount() {
        return data == null ? 0 : data.functions.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return data.functions.get(groupPosition).funs.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return data.functions.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return data.functions.get(groupPosition).funs.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupBinding binding = null;
        if(convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.group, null);
            binding = GroupBinding.bind(convertView);
            convertView.setTag(binding);
        } else {
            binding = (GroupBinding) convertView.getTag();
        }
        binding.setName(data.functions.get(groupPosition).name);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildBinding binding = null;
        if(convertView == null) {
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.child, null);
            binding = ChildBinding.bind(convertView);
            convertView.setTag(binding);
        } else {
            binding = (ChildBinding) convertView.getTag();
        }

        binding.setText(data.functions.get(groupPosition).funs.get(childPosition));
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
